namespace CodeConsult.Nordic.nRF8001.ApplicationControlInterface.Commands.SystemCommands
{
    public class SetKeyCommand : AciCommandBase
    {
        public const byte OpCode = 0x1A;

        public byte KeyType { get; set; }
        public byte[] Key { get; set; }

        protected override void EncodeBuffer()
        {
            Error = Error || (KeyType != 0x00 && KeyType != 0x01);
            Error = Error || (KeyType == 0x01 && (Key == null || Key.Length != 6));

            if (Error) return;

            var keyLength = Key != null ? Key.Length : 0;

            EncodeBufferWithCommandHeader(OpCode, 1 + keyLength);
            EncodeHelper.SetByte(KeyType);
            EncodeHelper.SetBytes(Key);
        }
    }
}
